﻿$(function () {
    $("#recreation-grid").datagrid({
        url: $.Api("Tour/Recreation/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#recreation-tools",
        sortName: "UpdateTime", sortOrder: "desc",
        columns:
        [[
            { field: "Name", title: "娱乐名称", width: 280, align: "center", sortable: true },
            { field: "Type", title: "娱乐类型", width: 100, align: "center", sortable: true },
            { field: "Price", title: "参考价格", width: 100, align: "center", sortable: true },
            { field: "Address", title: "娱乐地点", width: 280, align: "center", sortable: true },
            { field: "InsertTime", title: "添加时间", width: 140, align: "center", sortable: true },
            { field: "UpdateTime", title: "更新时间", width: 140, align: "center", sortable: true }
        ]]
    });

    $("#recreation-searchbox").searchbox({
        width: 150, prompt: "请输入娱乐名称查询",
        searcher: function (value) {
            $("#recreation-grid").datagrid("clearChecked");
            $("#recreation-grid").datagrid("load", { search: value });
        }
    });
});

var Recreation = {
    //添加
    Insert: function () {
        Recreation.Mutual("添加", "tour/recreation/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#recreation-grid").datagrid("getSelected");
        if (row) {
            Recreation.Mutual("修改", "tour/recreation/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#recreation-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Tour/Recreation/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Recreation.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 668,
            dialogid: "recreation-dialog", handler: function () {
                Recreation.ImgSet();
                $.ModelForm({
                    validate: true, button: $(this),
                    editor: "#recreation-content",
                    id: "#recreation-model",
                    url: $.Api("Tour/Recreation/Save"),
                    success: function (result) { Recreation.Reload(); }
                });
            },
            onLoad: function () { Recreation.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $.EditorInit("#recreation-content", 200, "p", true);

        var imgboxs = $(".recreation-img", $("#recreation-imgbox"));

        $(imgboxs).click(function () { Recreation.ImgClick(this); });
    },

    //预览
    Preview: function () {
        var row = $("#recreation-grid").datagrid("getSelected");
        if (row) {
            window.open("../Recreation.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#recreation-grid");
    },

    //上传图片
    Upload: function () {
        if (Recreation.ImgCount() < 3) {
            KindEditor.editor({
                uploadJson: $.Api("Tour/Recreation/Upload"),
                formatUploadUrl: false
            }).loadPlugin("image", function () {
                this.plugin.imageDialog({
                    showRemote: false,
                    clickFn: function (url, title, width, height, border, align) {
                        var box = $("#recreation-imgbox").show();
                        var imxpadding = $("<div></div>").addClass("recreation-imgpadding");
                        var img = "<img width='168' height='100' src='" + url + "' />";
                        var imgbox = $("<div url='" + url + "' type='Img'>" + img + "</div>");
                        imgbox.click(function () { Recreation.ImgClick(this); });
                        imgbox.addClass("recreation-img").addClass("panel-body").appendTo(imxpadding);
                        imxpadding.appendTo(box);
                        $.AdjustmentDialog($("#recreation-dialog")); this.hideDialog();
                    }
                });
            });
        }
        else { $.EasyuiMsg("最多能上传三张照片!"); }
    },
    //批量上传
    Batch: function () {
        if (Recreation.ImgCount() < 3) {
            KindEditor.editor({
                uploadJson: $.Api("Tour/Recreation/Upload"),
                formatUploadUrl: false
            }).loadPlugin("multiimage", function () {
                this.plugin.multiImageDialog({
                    clickFn: function (urlList) {
                        var box = $("#recreation-imgbox").show();
                        var imgcount = Recreation.ImgCount();
                        $.each(urlList, function (i, data) {
                            if ((3 - imgcount) > i) {
                                var imxpadding = $("<div></div>").addClass("recreation-imgpadding");
                                var img = "<img width='168' height='100' src='" + data.url + "' />";
                                var imgbox = $("<div url='" + data.url + "' type='Img'>" + img + "</div>");
                                imgbox.click(function () { Recreation.ImgClick(this); });
                                imgbox.addClass("recreation-img").addClass("panel-body").appendTo(imxpadding);
                                imxpadding.appendTo(box);
                            }
                        });
                        if (imgcount + urlList.length > 3) {
                            $.EasyuiSlide("最多能上传三张照片,超过部分已被忽略!");
                        }
                        $.AdjustmentDialog($("#recreation-dialog")); this.hideDialog();
                    }
                });
            });
        }
        else { $.EasyuiMsg("最多能上传三张照片!"); }
    },
    //设为封面
    Front: function () {
        var imgbox = $(".tree-node-selected", $("#recreation-imgbox"));
        if (imgbox.length > 0) {
            $.EasyuiConfirm("确认设置封面?", function (del) {
                if (del) {
                    var exfront = $(".recreation-front", $("#recreation-imgbox"));
                    if (exfront.length > 0) {
                        exfront.eq(0).parent().attr("type", "Img");
                        exfront.eq(0).remove();
                    }
                    var front = $("<div></div>").addClass("recreation-front");
                    front.addClass("icon-pictures").appendTo(imgbox.eq(0));
                    imgbox.eq(0).attr("type", "Front");
                }
            });
        }
        else { $.EasyuiSlide("请选择设置的图片!"); }
    },
    //单击事件
    ImgClick: function (img) {
        var imgboxs = $(".recreation-img", $("#recreation-imgbox"));

        for (var i = 0; i < imgboxs.length; i++) {
            if ($(imgboxs[i]).hasClass("tree-node-selected")) {
                $(imgboxs[i]).removeClass("tree-node-selected");
            }
        }

        $(img).addClass("tree-node-selected");
    },
    //删除图片
    ImgDelete: function () {
        var imgbox = $(".tree-node-selected", $("#recreation-imgbox"));
        if (imgbox.length > 0) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    imgbox.eq(0).parent().remove();
                    $.AdjustmentDialog($("#recreation-dialog"));
                }
            });
        }
        else { $.EasyuiSlide("请选择删除的图片!"); }
    },
    //删除全部
    ImgClear: function () {
        var imgbox = $(".recreation-img", $("#recreation-imgbox"));
        if (imgbox.length > 0) {
            $.EasyuiConfirm("确认删除全部图片?", function (del) {
                if (del) {
                    $("#recreation-imgbox").html("");
                    $.AdjustmentDialog($("#recreation-dialog"));
                }
            });
        }
        else { $.EasyuiSlide("没有任何图片!"); }
    },
    //图片前移
    ImgPrev: function () {
        var imgbox = $(".tree-node-selected", $("#recreation-imgbox"));
        if (imgbox.length > 0) {
            var parent = imgbox.eq(0).parent();
            var prev = imgbox.eq(0).parent().prev();
            if (prev.length > 0) {
                prev.eq(0).insertAfter(parent);
            }
            else { $.EasyuiSlide("到头啦!"); }
        }
        else { $.EasyuiSlide("请选择移动的图片!"); }
    },
    //图片后移
    ImgNext: function () {
        var imgbox = $(".tree-node-selected", $("#recreation-imgbox"));
        if (imgbox.length > 0) {
            var parent = imgbox.eq(0).parent();
            var next = imgbox.eq(0).parent().next();
            if (next.length > 0) {
                next.eq(0).insertBefore(parent);
            }
            else { $.EasyuiSlide("到头啦!"); }
        }
        else { $.EasyuiSlide("请选择移动的图片!"); }
    },
    //写入表单
    ImgSet: function () {
        var imgboxs = $(".recreation-img", $("#recreation-imgbox"));

        var arrimgs = []

        for (var i = 0; i < imgboxs.length; i++) {
            arrimgs.push({
                Img: $(imgboxs[i]).attr("url"),
                IsCover: $(imgboxs[i]).attr("type") == "Front"
            });
        }

        $("#recreation-images").val(JSON.stringify(arrimgs));
    },
    //图片数量
    ImgCount: function () {
        var imgboxs = $(".recreation-img", $("#recreation-imgbox"));

        return imgboxs.length;
    }
};